package com.imooc.miaosha.util;

import org.apache.commons.codec.digest.DigestUtils;

public class MD5Util {
    //静态的salt，用于第一次MD5
    private static final String salt = "1a2b3c4d";

    public static String md5(String src){
        return DigestUtils.md5Hex(src);
    }

    /**
     * 第一次MD5处理
     * @param inputPass
     * @return
     */
    public static String inputPassToFormPass(String inputPass){
        //这里没加“”出现了问题？？？
        String pass ="" + salt.charAt(1) + salt.charAt(7) + inputPass
                + salt.charAt(3) + salt.charAt(5);
        //System.out.println(pass);
        return md5(pass);
    }

    /**
     * 第二次MD5处理
     * @param formPass
     * @param salt
     * @return
     */
    public static String formPassToDBPass(String formPass,String salt){
        String pass = "" + salt.charAt(1) + salt.charAt(7) + formPass
                + salt.charAt(3) + salt.charAt(5);
        return md5(pass);
    }

    public static String inputPassToDBPass(String inputPass,String saltDB){
        return formPassToDBPass(inputPassToFormPass(inputPass),saltDB);
    }

    public static void main(String[] args) {
        System.out.println(inputPassToFormPass("123456"));//ebb42cba98d165b776b1597afb62bd50
        System.out.println(inputPassToDBPass("123456","1a2b3c4d"));//5f49274b4469b3375d2f20dc651addcf

        System.out.println("" + salt.charAt(0) + salt.charAt(1));
        System.out.println(salt.charAt(0) + salt.charAt(1));
    }

    public static String inputPassToDbPass(String inputPass, String saltDB) {
        String formPass = inputPassToFormPass(inputPass);
        String dbPass = formPassToDBPass(formPass, saltDB);
        return dbPass;
    }
}
